package com.wd.tech.activity.fragment;

import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.wd.tech.R;
import com.wd.tech.activity.activity.AddFriendActivity;
import com.wd.tech.activity.base.BaseFragment;
import com.wd.tech.activity.base.BasePreantert;
import com.wd.tech.activity.fragment.news_message.ContactPersonFragment;
import com.wd.tech.activity.fragment.news_message.MessageFragment;

import org.w3c.dom.Text;

import java.util.ArrayList;

import butterknife.BindView;


/**
 * @author 时文豪
 * @description:
 * @date :2020/7/2 12:09
 */
public class NewsFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.message_rb)
    RadioButton messageRb;
    @BindView(R.id.contacts_rb)
    RadioButton contactsRb;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.add)
    ImageView add;
    private PopupWindow popupWindow;
    private ArrayList<Fragment> list = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.news;
    }

    @Override
    protected BasePreantert initPreantert() {
        return null;
    }

    @Override
    protected void initData() {
        list.add(new MessageFragment());
        list.add(new ContactPersonFragment());
        setAdapter();
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
    }

    public void getData() {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow, null);
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setContentView(contentView);
        LinearLayout hy = contentView.findViewById(R.id.tv_hy);
        LinearLayout qun = contentView.findViewById(R.id.qun);
        hy.setOnClickListener(this);
        qun.setOnClickListener(this);
        View rootview = LayoutInflater.from(getActivity()).inflate(R.layout.news, null);
        popupWindow.showAtLocation(rootview, Gravity.TOP, 400, 200);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_hy:
                Toast.makeText(getContext(), "加好友", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                Intent intent = new Intent(getActivity(), AddFriendActivity.class);
                startActivity(intent);
                break;
            case R.id.qun:
                Toast.makeText(getContext(), "加群聊", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
                break;
        }

    }


    private void setAdapter() {
        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.message_rb: {
                        vp.setCurrentItem(0);
                        break;
                    }
                    case R.id.contacts_rb: {
                        vp.setCurrentItem(1);
                    }
                    break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + checkedId);
                }
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rg.check(rg.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView(View view) {

    }


}
